//
//  AinvitiViewController.m
//  Distributor
//
//  Created by 主事丫环 on 2019/5/14.
//  Copyright © 2019 主事丫环. All rights reserved.
//

#import "AinvitiViewController.h"
#import "HmSelectAdView.h"
#import "JSHAREService.h"
//#import <AdSupport/AdSupport.h>
#import "ShareView.h"
#import "NetLoadAreaPickerViewController.h"

@interface AinvitiViewController ()
@property (weak, nonatomic) IBOutlet UITextField *phoneTF;
@property (weak, nonatomic) IBOutlet UILabel *seleLB;
@property (weak, nonatomic) IBOutlet UISwitch *myswitch;
@property(nonatomic,strong)NSString *shengshiqu;

@property (nonatomic, strong) NSString *currentProvince;
@property (nonatomic, strong) NSString *currentCity;
@property (nonatomic, strong) NSString *currentArea;
@property (nonatomic, copy) NSString *currentDistanceId;
@end

@implementation AinvitiViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithHexString:@"#F2F2F2"];
    self.title = @"被邀请伙伴信息";
    [self.leftbutton setImage:imgname(@"left_icon") forState:(UIControlStateNormal)];
    // Do any additional setup after loading the view from its nib.
}

- (IBAction)switchaction:(UISwitch *)sender {
   
    UIAlertController *alertCon = [UIAlertController alertControllerWithTitle:@"每个区域只能设置一个管理员" message:@"确定设置？" preferredStyle:UIAlertControllerStyleAlert];
    
    [alertCon addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        KMyLog(@"取消");
        sender.on = NO;
    }]];
    
    [alertCon addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        KMyLog(@"确定");
        sender.on = YES;
    }]];
    
    [self presentViewController:alertCon animated:YES completion:nil];
}

- (IBAction)seleQUyuaction:(UIButton *)sender {
    NetLoadAreaPickerViewController * loadAreaVC = [[NetLoadAreaPickerViewController alloc]init];
    loadAreaVC.selectedBlock = ^(NetLoadAreaModel *areaModel) {
        self.currentProvince = areaModel.provinceName;
        NSLog(@"hahahahahahahahaha %@", self.currentProvince)
        self.currentCity = areaModel.cityName;
        self.currentArea = areaModel.districtName;
        self.currentDistanceId = areaModel.districtCode;
        self.seleLB.text = [NSString stringWithFormat:@"%@ %@ %@", self.currentProvince, self.currentCity, self.currentArea];
    };
    
    if ([[[UIDevice currentDevice] systemVersion] floatValue]>=8.0) {
        loadAreaVC.modalPresentationStyle=UIModalPresentationOverCurrentContext;
    } else {
        loadAreaVC.modalPresentationStyle=UIModalPresentationCurrentContext;
    }
    [self presentViewController:loadAreaVC animated:NO completion:^{
    }];
    return;
//    HmSelectAdView *selectV = [[HmSelectAdView alloc] initWithLastContent:self.currentProvince ? @[self.currentProvince, self.currentCity, self.currentArea] : nil];
//    selectV.confirmSelect = ^(NSArray *address) {
//
//        self.currentProvince = address[0];
//        self.currentCity = address[1];
//        self.currentArea = address[2];
//
//        self.seleLB.text = [NSString stringWithFormat:@"%@ %@",self.currentCity,self.currentArea];
//    };
//    [selectV show];
}

- (IBAction)tijsoaction:(UIButton *)sender {
    
    if (![self.phoneTF.text isValidMobileNumber]) {
        ShowToastWithText(@"请输入正确格式的手机号");
        return;
    }
    
    if (strIsEmpty(self.currentDistanceId)) {
        ShowToastWithText(@"请选择地区");
        return;
    }
    
    NSMutableDictionary *param = [NSMutableDictionary dictionaryWithCapacity:1];
    param[@"phone"] = _phoneTF.text;
    param[@"area"] = [NSString stringWithFormat:@"%@", self.currentDistanceId];

    NSString *isde = @"0";
    if (self.myswitch.on) {
        isde = @"1";
    }
    param[@"setLevel"] = isde;

    [NetWorkTool POST:invitationAgentLevelB param:param success:^(id dic) {
        ShowToastWithText(@"添加成功");
        if (self.myblock) {
            self.myblock(@"");
        }
        [self share];
        //[self.navigationController popViewControllerAnimated:YES];
    } other:^(id dic) {
        ShowToastWithText(dic[@"msg"]);
    } fail:^(NSError *error) {
        
    } needUser:YES];
}

- (void)share{
    NSMutableArray *titlearr     = [NSMutableArray arrayWithCapacity:5];
    NSMutableArray *imageArr     = [NSMutableArray arrayWithCapacity:5];
    NSMutableArray *typeArr     = [NSMutableArray arrayWithCapacity:5];
    [titlearr addObjectsFromArray:@[@"微信", @"微信朋友圈"]];
    [imageArr addObjectsFromArray:@[@"weixin22",@"weixin11"]];
    
    ShareView *shareView = [[ShareView alloc] initWithShareHeadOprationWith:titlearr andImageArry:imageArr andProTitle:@"分享到"];
    [shareView setBtnClick:^(NSInteger btnTag) {
        NSLog(@"\n点击第几个====%d\n当前选中的按钮title====%@",(int)btnTag,titlearr[btnTag]);
        [self shareWitipe:btnTag];
    }];
    [[UIApplication sharedApplication].keyWindow addSubview:shareView];
}

-(void)shareWitipe:(NSInteger )ind{
    ind++;
    //[MBProgressHUD showHUDAddedTo:self.view animated:YES];
    NSString *typestr = ind ==0?@"JSHAREPlatformWechatSession":@"JSHAREPlatformWechatTimeLine";
    
    NSArray *type = @[@"JSHAREPlatformWechatSession",@"JSHAREPlatformWechatTimeLine"];
    
    JSHAREMessage *message = [JSHAREMessage message];
    message.mediaType = JSHARELink;
    NSString *invitation_code =[NSString stringWithFormat:@"%@",[USER_DEFAULT objectForKey:@"invitation_code"]];
    //NSString *invitUrl = [NSString stringWithFormat:@"https://www.zzzsyh.com/index.html#/B_joining?invitationCode=%@", invitation_code];
    //NSLog(@"%@", invitUrl);
    message.url = @"https://www.zzzsyh.com/index.html#/A_Joining";
    message.text = [NSString stringWithFormat:@"时间:%@ 主事Y环邀您一起赚钱",[self localizedStringTime]];
    message.title = @"主事Y环邀您一起赚钱";
    message.platform = ind;
    NSString *imageURL = @"http://img2.3lian.com/2014/f5/63/d/23.jpg";
    NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:imageURL]];
    
    message.image = imageData;
    [JSHAREService share:message handler:^(JSHAREState state, NSError *error) {
        //[self showAlertWithState:state error:error];
        KMyLog(@"sdf%@",error);
        //[MBProgressHUD hideHUDForView:self.view animated:YES];
        if (!error) {
            ShowToastWithText(@"分享成功");
        }else{
            ShowToastWithText(@"分享失败");
            NSLog(@"分享失败, error : %@", error);
        }
    }];
}

- (NSString *)localizedStringTime{
    NSDateFormatter*formatter = [[NSDateFormatter alloc]init];[formatter setDateFormat:@"yyy-MM-dd HH:mm:ss"];
    NSString*dateTime = [formatter stringFromDate:[NSDate date]];
    return dateTime;
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
